package com.it;

import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("following")
@CrossOrigin
public class FollowingController {

    @Resource
    private FollowingService followingService;

    @PostMapping("")
    public R<?> save(@RequestParam("userId") Integer userId, @RequestParam("followId") Integer followId) {
        if (userId == null || followId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return followingService.save(userId, followId) > 0 ? R.success() : R.error();
//        return null;
    }

    /**
     * @param userId        帖子的主人的用户id
     * @param currentUserId 当前用户id
     * @return
     */
    @GetMapping("")
    public R<?> checkFollow(@RequestParam("userId") Integer userId, @RequestParam("currentUserId") Integer currentUserId) {
        if (userId == null || currentUserId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("checkFollow");
        return followingService.checkFollow(userId, currentUserId) ? R.success() : R.error("未关注");
    }

    /**
     * @param userId        帖子的主人的用户id
     * @param currentUserId
     * @return
     */
    @PutMapping("")
    public R<?> cancelFollow(@RequestParam("userId") Integer userId, @RequestParam("currentUserId") Integer currentUserId) {
        if (userId == null || currentUserId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return followingService.updateFollow(userId, currentUserId) > 0 ? R.success() : R.error();
    }
}
